import { request } from "../../request/request.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [{
                id: 0,
                value: '综合',
                isActive: true
            },
            {
                id: 1,
                value: '销量',
                isActive: false
            },
            {
                id: 2,
                value: '价格',
                isActive: false
            }
        ],
        // 商品参数
        QueryParams: {
            query: "",
            cid: "",
            pagenum: 1,
            pagesize: 10
        },
        // 商品列表
        goodsList: [],
        // 总数据条数
        total: 0,
        //总页数
        totalPage: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        // const { cid } = this.data.params
        this.data.QueryParams.cid = options.cid;
        // console.log(typeof cid)
        this.getGoodsList()

    },
    // tabs点击切换事件
    handleTabsItemChange(e) {
        //获取子组件传递的值
        const index = e.detail;
        let { tabs } = this.data;
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
        this.setData({
            tabs
        })
    },
    // 发请求获取数据
    getGoodsList() {
        request({ url: "/goods/search", data: this.data.QueryParams })
            .then((result) => {
                console.log(result)
                const total = result.data.message.total
                let goodsList = [...this.data.goodsList, ...result.data.message.goods]
                this.setData({
                    goodsList,
                    total
                })
            })
            //关闭下拉刷新
        wx.stopPullDownRefresh();
    },
    // 滚动条触底事件
    onReachBottom() {
        this.data.totalPage = Math.ceil(this.data.total / this.data.QueryParams.pagesize)
        if (this.data.QueryParams.pagenum >= this.data.totalPage) {
            return wx.showToast({
                title: '到底了',
                icon: 'error',
                duration: 2000
            })
        } else {
            this.data.QueryParams.pagenum++;
            this.getGoodsList()
        }
    },
    // 下拉刷新
    onPullDownRefresh() {
        //重置数据
        this.setData({
                goodsList: []
            })
            // 重置页码
        this.data.QueryParams.pagenum = 1;
        this.getGoodsList()
    }
})